package com.xmotor.test_sign_spring_boot_starter.client;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.demo.util.SignUtil;
import com.xmotor.test_sign.client.HttpConnectionPoolUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Description 测试签名组件客户端
 * @Author Dang Xianyue
 * @Date 2020-08-09 16:01
 * @Version 1.0
 */
@Slf4j
public class TestSignSpringBootStarterDefaultClient {
    private String baseUrl;
    private String secretKey;

    private TestSignSpringBootStarterDefaultClient(String baseUrl, String secretKey) {
        this.baseUrl = baseUrl;
        this.secretKey = secretKey;
    }

    private static volatile TestSignSpringBootStarterDefaultClient client = null;

    public static TestSignSpringBootStarterDefaultClient getInstance(String baseUrl, String secretKey){
        if (client == null){
            synchronized (TestSignSpringBootStarterDefaultClient.class){
                if (client == null){
                    client = new TestSignSpringBootStarterDefaultClient(baseUrl,secretKey);
                }
            }
        }
        return client;
    }

    public TestSignSpringBootStarterResponse doPostJson(TestSignSpringBootStarterRequest request){
        try {
            DateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            DateFormat sdf2 = new SimpleDateFormat("yyyyMMddHHmm");

            JSONObject body = request.getBody();
            if (body != null) {
                Date now = new Date();
                body.put("signTime", sdf2.format(now));
                String sign = SignUtil.sign(body, secretKey);
                body.remove("signTime");

                JSONObject params = new JSONObject();
                params.put("body", body);
                params.put("time", sdf1.format(now));
                params.put("sign", sign);

                log.info("调用接口【" + request.getMethod() + "】请求参数：" + params.toString());
                String result = HttpConnectionPoolUtil.post(baseUrl + request.getMethod(), JSON.toJSONString(params));

                if (StringUtils.isNotEmpty(result)) {
                    log.info("调用接口【" + request.getMethod() + "】成功，返回结果：" + result);
                    return JSONObject.parseObject(result, TestSignSpringBootStarterResponse.class);
                } else {
                    log.info("调用接口【" + request.getMethod() + "】失败，result is null");
                }
            } else {
                log.info("调用接口【" + request.getMethod() + "】失败，body is null");
            }
        } catch (Exception e) {
            log.error("Exception", e);
        }

        return null;
    }

    public TestSignSpringBootStarterResponse doPostForm(TestSignSpringBootStarterRequest request){
        try {
            DateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            DateFormat sdf2 = new SimpleDateFormat("yyyyMMddHHmm");

            JSONObject body = request.getBody();
            if (body != null) {
                Date now = new Date();
                body.put("signTime", sdf2.format(now));
                String sign = SignUtil.sign(body, secretKey);
                body.remove("signTime");

                JSONObject params = new JSONObject();
                params.put("body", JSON.toJSONString(body));
                params.put("time", sdf1.format(now));
                params.put("sign", sign);

                log.info("调用接口【" + request.getMethod() + "】请求参数：" + params.toString());
                String result = HttpConnectionPoolUtil.post(baseUrl + request.getMethod(), params);

                if (StringUtils.isNotEmpty(result)) {
                    log.info("调用接口【" + request.getMethod() + "】成功，返回结果：" + result);
                    return JSONObject.parseObject(result, TestSignSpringBootStarterResponse.class);
                } else {
                    log.info("调用接口【" + request.getMethod() + "】失败，result is null");
                }
            } else {
                log.info("调用接口【" + request.getMethod() + "】失败，body is null");
            }
        } catch (Exception e) {
            log.error("Exception", e);
        }

        return null;
    }

}
